#include "pagepismodesetting.h"
#include "ui_pagepismodesetting.h"

PagePisModeSetting::PagePisModeSetting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PagePisModeSetting)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);
    setPageActive(false);
    current_pis_mode_ = PIS_ModeAuto;
    btns_enable_ = false;
    current_direction_ = 1;
    ui->lineChangeHint->hide();
}

PagePisModeSetting::~PagePisModeSetting()
{
    delete ui;
}

void PagePisModeSetting::setPageActive(bool xActive)
{
    btns_enable_ = xActive;
    ui->Btn_PisAuto->setEnabled(xActive);
    ui->Btn_PisSemiAuto->setEnabled(xActive);
    ui->Btn_PisManual->setEnabled(xActive);
    ui->Btn_ModeConfirm->setEnabled(xActive);
    ui->Btn_PisDownWard->setEnabled(xActive);
    ui->Btn_PisUpWard->setEnabled(xActive);
    ui->Btn_DirectionConfirm->setEnabled(xActive);
}

void PagePisModeSetting::on_Btn_PisAuto_clicked(bool checked)
{
    if(checked) current_pis_mode_ = PIS_ModeAuto;
}

void PagePisModeSetting::on_Btn_PisSemiAuto_clicked(bool checked)
{
    if(checked) current_pis_mode_ = PIS_ModeSemiAuto;
}

void PagePisModeSetting::on_Btn_PisManual_clicked(bool checked)
{
    if(checked) current_pis_mode_ = PIS_ModeMannual;
}

uint8_t PagePisModeSetting::getCurrentPISMode() const
{
    return current_pis_mode_;
}

uint8_t PagePisModeSetting::getCurrentDirection() const
{
    return current_direction_;
}

void PagePisModeSetting::on_Btn_PisUpWard_pressed()
{
    current_direction_ =  1;
}

void PagePisModeSetting::on_Btn_PisDownWard_pressed()
{
    current_direction_ =  2;
}

void PagePisModeSetting::on_Btn_ModeConfirm_clicked()
{
    emit signal_set_pis_mode(current_pis_mode_,true);
    ui->Btn_ModeConfirm->setEnabled(false);
    ui->Btn_PisAuto->setEnabled(false);
    ui->Btn_PisManual->setEnabled(false);
    ui->Btn_PisSemiAuto->setEnabled(false);
    QTimer::singleShot(3000,this,[=](){
        emit signal_set_pis_mode(current_pis_mode_,false);
        ui->Btn_ModeConfirm->setEnabled(true);
        ui->Btn_PisAuto->setEnabled(true);
        ui->Btn_PisManual->setEnabled(true);
        ui->Btn_PisSemiAuto->setEnabled(true);
    });
}

void PagePisModeSetting::syncPisMode(int mode)
{
    if(mode == PIS_ModeAuto)
        ui->CurrentPisMode->setText("全自动");
    else if(mode == PIS_ModeSemiAuto)
        ui->CurrentPisMode->setText("半自动");
    else if(mode == PIS_ModeMannual)
        ui->CurrentPisMode->setText("手动");

    if(GlobalConfig::cabActivated == GlobalConfig::cabNumber && !GlobalConfig::xFAM)
        return;
    ui->Btn_PisAuto->setChecked(mode==PIS_ModeAuto);
    ui->Btn_PisSemiAuto->setChecked(mode==PIS_ModeSemiAuto);
    ui->Btn_PisManual->setChecked(mode==PIS_ModeMannual);
    current_pis_mode_ = mode;
    emit signal_set_pis_mode(current_pis_mode_,false);
}

void PagePisModeSetting::syncLineDirection(uint8_t dir)
{
    if(dir == 1)
        ui->CurrentDirection->setText("上行");
    else if(dir == 2)
        ui->CurrentDirection->setText("下行");
    else if(dir == 0)
    {
        ui->CurrentDirection->setText("?");
        return;
    }

    if(GlobalConfig::cabActivated == GlobalConfig::cabNumber && !GlobalConfig::xFAM)
        return;
    emit signal_change_direction(dir);
    ui->Btn_PisUpWard->setChecked(dir==1);
    ui->Btn_PisDownWard->setChecked(dir==2);
}

void PagePisModeSetting::on_Btn_DirectionConfirm_clicked()
{
    emit signal_change_direction(current_direction_);
    static uint8_t old_dir = 1;
    if(old_dir != current_direction_)
    {
        ui->lineChangeHint->show();
        QTimer::singleShot(6000,this,[=](){
            ui->lineChangeHint->hide();
        });
        old_dir = current_direction_;
    }
}
